Skip to content

fix(v2): retain legacy function_call in dump_message when content is empty - #2484

Closed
cognis-digital wants to merge 1 commit into
567-labs:mainfrom
cognis-digital:fix/dump-message-empty-content-function-call
Closed

fix(v2): retain legacy function_call in dump_message when content is empty#2484
cognis-digital wants to merge 1 commit into
567-labs:mainfrom
cognis-digital:fix/dump-message-empty-content-function-call

Conversation

@cognis-digital

Copy link
Copy Markdown

Problem

dump_message (instructor/v2/core/messages.py) only appended the legacy OpenAI function_call when the assistant message had truthy content. Tool/function-call responses commonly use content=None, so the serialized message dropped the function call entirely.

Because dump_message builds the assistant turn on the retry/reask path (OpenAI and Mistral handlers), a re-ask could omit the exact structured output that caused the validation failure, leaving the next request without the relevant assistant response.

Fixes #2464.

Repro (before)

from openai.types.chat import ChatCompletionMessage
from openai.types.chat.chat_completion_message import FunctionCall
from instructor.v2.core.messages import dump_message

msg = ChatCompletionMessage(role="assistant", content=None,
                            function_call=FunctionCall(name="lookup", arguments='{"id":7}'))
print(dump_message(msg))
# {'role': 'assistant', 'content': ''}   # function_call lost

Fix

Remove the falsy-content guard so a present function_call is always serialized. Behavior for non-empty string content and for list content (text/refusal flattening) is unchanged.

Tests

Added test_dump_message_retains_function_call_when_content_is_empty, which fails on main and passes with this change. Existing dump_message tests and the surrounding message/handler suites remain green. CHANGELOG updated under [Unreleased].

@jxnl

jxnl commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Consolidated and shipped in #2495. Closing this focused patch as superseded; thank you for the contribution.

@jxnl jxnl closed this Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dump_message drops legacy function calls when content is empty

2 participants